﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using XWG.Framework.AuthUtility;
using System.Data;
using Newtonsoft.Json.Converters;

namespace XWG.JWBST.AppealProcess
{
    /// <summary>
    /// 诉求直通
    /// </summary>
    public class Process : IProcess
    {
        /// <summary>
        /// 诉求直通
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            //读取提交数据并UrlDecode解码
            string receivemes = HttpUtility.UrlDecode(reader.ReadToEnd());
            string json = string.Empty;
            string Unphone =string.Empty;
            //反序列化json对象    
            JObject ob = (JObject)JsonConvert.DeserializeObject(receivemes);
            JWBSTEntity.TbBstAppeal apInfo = new JWBSTEntity.TbBstAppeal();
            string processType = ob["type"].ToString().Replace("\"", string.Empty);
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            //提交诉求信息
            if (processType == "submit")
            {
                apInfo.Id = Guid.NewGuid().ToString();
                apInfo.Lsh = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.GetLSH("TB_BST_APPEAL");
                apInfo.Name = DES.Encode(ob["name"].ToString().Replace("\"", string.Empty));
                apInfo.Address = DES.Encode(ob["address"].ToString().Replace("\"", string.Empty));
                apInfo.Phone = DES.Encode(ob["phone"].ToString().Replace("\"", string.Empty));
                Unphone = ob["phone"].ToString().Replace("\"", string.Empty);
                apInfo.Email = DES.Encode(ob["email"].ToString().Replace("\"", string.Empty));
                apInfo.Police = ob["police"].ToString().Replace("\"", string.Empty);
                apInfo.Createdate = DateTime.Now;
                apInfo.Content = ob["content"].ToString().Replace("\"", string.Empty);
                apInfo.Deltag = 0;
                apInfo.Source = "3";
                apInfo.Movesign = "3";
                apInfo.Replysign = "0";
                apInfo.Isview = "0";
                apInfo.Tabletype = "1";
                ResultModel re = new ResultModel();
                re = JWBSTBLL.TbBstAppealBLL.Insert(apInfo, "", false);
                //记录请求信息
                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                visit.Id = Guid.NewGuid().ToString();
                visit.Phone = Unphone;
                visit.Time = DateTime.Now;
                visit.Modulename = "Appeal";
                visit.Optionname = "提交诉求信息";
                visit.Other = ob.ToString();
                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);
                return re.Message;
                
            }
            //根据手机号查询诉求历史
            if (processType == "History")
            {

                string phone = DES.Encode(ob["phone"].ToString().Replace("\"", string.Empty));
                Unphone = ob["phone"].ToString().Replace("\"", string.Empty);
                DataTable dt = JWBSTBLL.TbBstAppealBLL.GetInfoByPhone(phone);

                //记录请求信息
                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                visit.Id = Guid.NewGuid().ToString();
                visit.Phone = Unphone;
                visit.Time = DateTime.Now;
                visit.Modulename = "Appeal";
                visit.Optionname = "诉求信息历史查询";
                visit.Other = ob.ToString();
                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);


                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["NAME"] = DES.Decode(dt.Rows[i]["NAME"].ToString());
                        dt.Rows[i]["PHONE"] = DES.Decode(dt.Rows[i]["PHONE"].ToString());
                        dt.Rows[i]["EMAIL"] = DES.Decode(dt.Rows[i]["EMAIL"].ToString());
                        dt.Rows[i]["ADDRESS"] = DES.Decode(dt.Rows[i]["ADDRESS"].ToString());
                    }
                    json = JsonConvert.SerializeObject(dt, dtConverter);
                    return json;
                }
                else
                {
                    return "[]";
                }
            }
            else
            {
                try
                {
                    string id = ob["id"].ToString().Replace("\"", string.Empty);
                    QueryByPage Q = new QueryByPage();
                    Reply re = new Reply();
                    DataTable dtTbBstFastprocess = new DataTable();
                    string sqlwhere = "where GDID= '" + id + "'";
                    dtTbBstFastprocess = Q.GetRecordByPage("ZB_BLJG,ZB_BLRQ", sqlwhere, "ZB_BLRQ", 1, 1, "TB_MSJW_SQJG", "ID", "DESC").Tables[0];
                    if (dtTbBstFastprocess != null && dtTbBstFastprocess.Rows.Count > 0)
                    {
                        re.Content = dtTbBstFastprocess.Rows[0]["ZB_BLJG"].ToString();
                        re.data = dtTbBstFastprocess.Rows[0]["ZB_BLRQ"].ToString();
                        json = JsonConvert.SerializeObject(re, dtConverter);
                        return json;
                    }
                    else
                    {
                        JObject rejson = new JObject();
                        rejson.Add("Content", "");
                        json = JsonConvert.SerializeObject(rejson);
                        return json;
                        //return "[]";
                    }
                }
                catch (Exception ex)
                {

                    throw;
                }
            }

        }
    }

    public class Reply
    {
        public string Content { get; set; }
        public string data { get; set; }
    }
}
